#!/bin/bash
_XPG=1
result=0
errcnt=0
tstcnt=0;
if test "$1" == "--valgrind"; then
valgrind=`which valgrind`
shift
else
valgrind=
fi
diff -u $0 $0 >/dev/null 2>&1 && diff='diff -u' || diff='diff'
#diff="${diff} -I '#line [0-9]*'"
if uname|grep -q -i 'CYGWIN' && test -x "@WINBUILDDIR@/re2c.exe"; then
re2c='@WINBUILDDIR@/re2c.exe'
else
re2c='@builddir@/re2c'
fi
if test ! -x "${re2c}"; then
  echo "Cannot find re2c executable (${re2c})."
fi
if test -n "${valgrind}"; then
  valgrind="${valgrind} -q "
  echo "Using valgrind"
fi
re2c="${valgrind}${re2c}"
echo "Testing: ${re2c}"
if ${re2c} -v|grep -q 're2c @PACKAGE_VERSION@ *\r*'; then
echo "Version: @PACKAGE_VERSION@"
else
echo "Warning: test build for @PACKAGE_VERSION@ but testing `${re2c} -v`"
fi
echo "Diffing: ${diff}"
if test $# = 0; then
tests=`for i in test lessons; do find @top_srcdir@/$i -name '*.re'; done|sort`
else
tests="$@"
fi;
for x in $tests; do
	tstcnt=$(($tstcnt+1))
	switches=`basename $x|sed -e 's/^[^.]*\.\(.*\)\.re$/-\1/g' -e 's/^[^-].*//g' -e 's/\([^ ]\)--/\1 --/g'`
	genname=`echo $switches|sed -e 's,--.*$,,g' -e 's,^.[^o]*$,,g' -e 's,^[^ot]*t.*o.*$,,g' -e 's,^-[^o]*o\(.*\),@builddir@/test/\1,g'`
	headers=`echo $switches|sed -e 's,--.*$,,g' -e 's,^.[^t]*$,,g' -e 's,^[^ot]*o.*t.*$,,g' -e 's,^-[^t]*t\(.*\),@builddir@/test/\1,g'`
	switches=`echo $switches|sed -e 's,^-\([^ot-]*[ot]\)\(.*\)$,-\1@builddir@/test/\2,g'`
	# don't use the -o flag, since it makes it harder to diff.
	echo $x: $switches
	outname=@builddir@/test/`basename ${x%.re}.c.temp`
	outdiff=@builddir@/test/`basename ${x%.re}.c.diff`
	typname=@builddir@/test/`basename ${x%.re}.h.temp`
	typdiff=@builddir@/test/`basename ${x%.re}.h.diff`
	$re2c $switches $x 2>&1 | sed -e "s,$x,`basename $x`,g" -e "s,/\* Generated by re2c .*\*/,/\* Generated by re2c \*/,g" > $outname
	if test -n "$genname"; then
		cat $genname | sed -e 's,@builddir@/test/,,g' -e "s,/\* Generated by re2c .*\*/,/\* Generated by re2c \*/,g" > $outname
		rm $genname
	fi
	ok=1
	if test -n "$headers"; then
		cat $headers | sed -e 's,@builddir@/test/,,g' -e "s,/\* Generated by re2c .*\*/,/\* Generated by re2c \*/,g" > $typname
		rm $headers
		if test ! -f ${x%.re}.h; then
			echo "Missing: ${x%.re}.h"
			result=1
			errcnt=$(($errcnt+1))
		elif ${diff} ${x%.re}.h $typname > $typdiff; then
			echo "Passed header test."
			rm $typname
		else
			echo "Failed: ${x%.re}.h ${x%.re}.h.temp differ."
			result=1
			errcnt=$(($errcnt+1))
			ok=0
		fi
	fi
	if test ! -f ${x%.re}.c; then
		echo "Missing: ${x%.re}.c"
		result=1
		errcnt=$(($errcnt+1))
	elif ${diff} ${x%.re}.c $outname > $outdiff; then
		if test $ok = 1; then
			echo "Passed."
		fi
		rm $outname
	else
		echo "Failed: ${x%.re}.c ${x%.re}.c.temp differ."
		result=1
		errcnt=$(($errcnt+1))
	fi
	test -f $outdiff -a ! -s $outdiff && rm -f $outdiff
	test -f $typdiff -a ! -s $typdiff && rm -f $typdiff
done
if test $result = 0; then
	echo "All $tstcnt tests passed successfully."
else
	echo "Error: $errcnt out $tstcnt tests failed."
fi
exit $result
